import Vue from 'vue';
import axios from 'axios';
const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    baseURL: '',
    timeout: 5000,
});

service.interceptors.request.use( config => {
    return config;
}, error => {
    console.log(error);
    return Promise.reject();
});

service.interceptors.response.use(response => {
    if(response.status === 200) {
        return response.data;
    // } else if (response.status === 401){
    //     service.post('/logout',{}).then(response => {
    //         window.location.reload();
    //     });
    }else{
        Promise.reject();
    }
}, error => {
    switch (error.response.status) {
        // case 401:
        //     service.post('/logout',{}).then(response => {
        //         window.location.reload();
        //     });
        //     break;
        case 503:
            window.location.reload();
            break;
    }
    return Promise.reject();
});

//默认其他请求格式
export default service;
//get
export function get(url,params={}){
    return new Promise((resolve,reject) => {
        service.get(url,{
            params:params
        }).then(response => {
            resolve(response);
        }).catch(err => {
            reject(err)
        })
    })
}
//post
export function post(url,data = {}){
    return new Promise((resolve,reject) => {
        service.post(url,data).then(response => {
            resolve(response);
        }).catch(err => {
            reject(err)
        })
    })
}


